<?php

use Biz\cache\brand\BrandTypeChange as cBrandTypeChange;
use Biz\model\brand\Brand;
use Biz\model\wiki\ObjRelateWiki;
use Biz\model\wiki\Wiki as mWiki;
use Biz\service\wiki\ObjRelateWiki as sObjRelateWiki;
use Biz\service\wiki\Wiki as sWiki;
use Doraemon\model\Photos;
use Doraemon\tools\Log;

class ObjRelateWikiToEsAction extends ActionPlugin
{
    const IS_TRACING = true;

    const BRAND_WIKI_HANDLE_MAX_COUNT = 100;

    /**
     * @throws Exception
     */
    public function main()
    {
        $table = $this->messageBody['table'] ?? '';
        $data = [];
        if (strstr($table, ObjRelateWiki::_TABLE_NAME)) {
            $data = $this->messageBody['data'] ?? [];
            $wikiId = $data['wiki_id'];
            if ($data) {
                //#同步obj_relate_wiki 数据至es /brand/objrelatewikitoes#[{"$data":{"id":3449,"obj_id":"00010ns0100bonrg","obj_type":3,"rel_obj_id":"c90b921hc0u00v000qxeldbbonrg","rel_obj_type":1,"wiki_id":1696,"goods_id":0,"uid":19626316,"type":1,"status":9,"admin_uid":0,"addtime":"2021-08-07 06:49:40","edittime":"2022-04-29 02:55:49"}},{"trace":"df6b65cf7c1ea642c6bb387a02b493163207"},"/brand/objrelatewikitoes","GoFrameHTTPClient v1.16.6"]
                //更新至es
//                $res = sObjRelateWiki::syncWikiCommentToEs($data);
//                if (!$res) {
//                    Log::error('ObjRelateWikiToEsAction-更新失败', ['$wikiId' => $wikiId]);
//                }
                //更新商品计数器
                $spuInfo = sWiki::getWikiBaseInfos([$wikiId]);
                $spuInfo = ($spuInfo && isset($spuInfo[$wikiId])) ? $spuInfo[$wikiId] : [];
                $basicId = $spuInfo ? $spuInfo['basic_id'] : 0;
                $isSync = sObjRelateWiki::syncWikiCommentCounter($wikiId, $basicId);
                if (!$isSync) {
                    Log::error('计数器更新失败' . '$wikiId:' . $wikiId . '$basicId:' . $basicId);
                }
            }
        }
        /*if (strstr($table, Photos::_TABLE_NAME)) {
            $data = $this->messageBody['data'] ?? [];
            if ($data) {
                //更新note质量分数 /brand/objrelatewikitoes#[{"$data":{"id":"0009v3y0000booip","uid":19627297,"house_type":0,"type":0,"article_id":"","title":"这是标题小心翼翼","remark":"个人破婆婆婆婆哦早睡早起也是吾日三省吾身一下倪哦破婆婆婆婆说我婆婆婆婆哦婆婆哦婆婆婆婆婆婆婆婆我哦婆婆哦色婆婆哦破哦色色婆婆哦婆婆哦哦破婆婆婆婆in破破哦破婆婆婆婆也是一种自主性哦随心所欲全心全意一下治抑郁症","admin_tag":"","group_tag":"","quality":3,"admin_score":30,"pic_id":"10d8420ku1600XW00rb1qxpbooip","pic_org_id":"10d8420ku1600XW00rb1qxpbooip","video_id":"","pic_list":"[{\"pic_id\":\"10d8420ku1600XW00rb1qxpbooip\",\"pic_org_id\":\"10d8420ku1600XW00rb1qxpbooip\",\"has_tags\":0,\"has_wiki\":0,\"has_old_tags\":0,\"has_goods\":0},{\"pic_id\":\"9049720ku15m01w00rb1qxpbooip\",\"pic_org_id\":\"9049720ku15m01w00rb1qxpbooip\",\"has_tags\":0,\"has_wiki\":0,\"has_old_tags\":0,\"has_goods\":0}]","is_album":1,"has_goods":1,"mention_uids":"[]","show_type":0,"resource_type":0,"is_best":0,"is_bottom":0,"topic_activity_id":0,"addtime":1651144093,"edittime":0,"user_update_time":0,"add_ip":"172.30.5.1","status":1,"show_status":1,"is_origin":1,"image_type":"2","is_fold":0,"pic_num":2,"address_num":0,"address_name":"","activity_id":0,"img_tags":"","new_img_tags":"","subject":""}},{"trace":"87c13aba8950ac46d1d58a86a219105f8602"},"/brand/objrelatewikitoes","GoFrameHTTPClient v1.16.6"]
                //更新至es
                if ($data['id'] && $data['admin_score'] > 0) {
                    $res = sObjRelateWiki::syncAdminScoreToEs($data['id'], $data['admin_score']);
                    if (!$res) {
                        Log::error('ObjRelateWikiToEsAction-更新失败',
                            ['obj_id' => $data['id'], 'admin_score' => $data['admin_score']]);
                    }
                }
            }
        }*/

        //品牌过期或续费 更新品牌下商品的住友说以及品牌说 计数器
        if (strstr($table, Brand::_TABLE_NAME)) {
            $data = $this->messageBody['data'] ?? [];//{"id":2168,"brand_name":"jianwei品牌04","en_brand_name":"jianweipinpai04","logo":"","discription":"这是品牌与","detail":"这是品牌简介","location":"1,382","tag":"","tag_id":0,"status":1,"type":1,"sort":0,"uid":19626606,"store_status":1,"wiki_status":0,"suggest_status":0,"updated_time":"2022-05-26 03:09:10","created_time":"2022-02-18 03:34:34","brand_alias":"[\"游戏\",\"就是\",\"弟弟\"]","brand_type":2,"is_test":0,"pack_type":7,"card_image":"b42d420i20c00f300r7hk9bbonzi","package_id":18,"level":1,"growth_value":1085,"area_code":110105,"ob_tag":""}

            if ($data) {
                $brandId = $data['id'];
                $wikiList = mWiki::getInfosByBrandIds($brandId);
                $wikiIdArr = array_keys($wikiList);
                $objRelateWikiCount = ObjRelateWiki::count(['wiki_id' => $wikiIdArr]);
                //数量大的走定时任务去处理
                if ($objRelateWikiCount > self::BRAND_WIKI_HANDLE_MAX_COUNT) {
                    cBrandTypeChange::addBrand($brandId);
                    Log::info("brand_type_change_to_crond#brand_id=".$brandId,
                        [
                            'brand_id' => $brandId,
                            'rel_wiki_count' => $objRelateWikiCount
                        ]
                    );
                } else {
                    $objRelateWikiList = ObjRelateWiki::select('wiki_id', ['wiki_id' => $wikiIdArr]);
                    if ($objRelateWikiList) {
                        $objRelateWikiList = array_unique($objRelateWikiList);
                        foreach ($objRelateWikiList as $wikiId) {
                            if (isset($wikiList[$wikiId])) {
                                $basicId = $wikiList[$wikiId]['basic_id'] ?? 0;
                                if ($basicId > 0) {
                                    $res = sObjRelateWiki::syncWikiCommentCounter($wikiId, $basicId);
                                    if (!$res) {
                                        Log::error('过期或续费品牌-更新住友说计数器失败',
                                            ['wiki_id' => $wikiId, 'basic_id' => $basicId]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        $this->setSuccess($data);
    }
}